package org.ala.tiktools.tools.list;

import java.util.Iterator;

/**
 * 双链
 *
 * @author ala
 * @date 2025-04-12 23:19
 */
public class DoubleLinkedList<E> {


    /**
     *  数量
     */
    protected int size;
    /**
     *  首尾
     */
    protected Node<E> HEAD, TAIL;


    public DoubleLinkedList() {
        this.size = 0;
        this.HEAD = new Node<>(null);
        this.TAIL = new Node<>(null);
        HEAD.next = TAIL;
        TAIL.prev = HEAD;
    }


    /**
     *  首插
     */
    public void addFirst(E v) {
        addFirst(new Node<>(v));
    }
    /**
     *  首插
     */
    public void addFirst(Node<E> n) {
        Node<E> nxt = HEAD.next;
        HEAD.next = n; n.next = nxt;
        nxt.prev = n; n.prev = HEAD;
        size++;
    }
    /**
     *  尾插
     */
    public void addLast(E v) {
        addLast(new Node<>(v));
    }
    /**
     *  尾插
     */
    public void addLast(Node<E> n) {
        Node<E> prv = TAIL.prev;
        prv.next = n; n.next = TAIL;
        TAIL.prev = n; n.prev = prv;
        size++;
    }
    /**
     *  删除节点
     */
    public void remove(Node<E> n) {
        Node<E> prv = n.prev, nxt = n.next;
        prv.next = nxt; nxt.prev = prv;
        size--;
    }
    /**
     *  后插
     */
    public void insertAfter(Node<E> n, E v) {
        Node<E> node = new Node<>(v);
        Node<E> nxt = n.next;
        n.next = node; node.next = nxt;
        nxt.prev = node; node.prev = n;
        size++;
    }


    /**
     *  首节点
     */
    public Node<E> firstNode() {
        return HEAD.next;
    }
    /**
     *  尾节点
     */
    public Node<E> lastNode() {
        return TAIL.prev;
    }
    /**
     *  首节点值
     */
    public E peek() { return HEAD.next.val; }
    /**
     *  首节点值，并删除首节点
     */
    public E pop() {
        Node<E> n = HEAD.next;
        remove(n);
        return n.val;
    }
    

	/**
     *  节点数
     */
    public int size() {
        return size;
    }
    /**
     *  是否为空
     */
    public boolean isEmpty() { return size == 0; }


    public Node<E> getHEAD() {
        return HEAD;
    }

    public void setHEAD(Node<E> HEAD) {
        this.HEAD = HEAD;
    }

    public Node<E> getTAIL() {
        return TAIL;
    }

    public void setTAIL(Node<E> TAIL) {
        this.TAIL = TAIL;
    }
    
    public void clear() {
    	//	清掉头尾针
    	HEAD = TAIL = null;
    	size = 0;
    }
    
    /**
     * 	迭代器
     */
    public static class DoubleLinkedListIterator<E> implements Iterator<E> {
    	/**
    	 * 	首尾节点
    	 */
    	protected Node<E> HEAD, TAIL;
    	protected DoubleLinkedListIterator(Node<E> HEAD, Node<E> TAIL) {
    		this.HEAD = HEAD;
    		this.TAIL = TAIL;
    	}
    	/**
    	 * 	当前结点
    	 */
    	protected Node<E> crt = null;
    	/**
    	 * 	重置
    	 */
    	protected void reset() {
    		crt = HEAD.next;
    	}
    	/**
    	 * 	删除当前迭代
    	 */
    	
    	
		@Override
		public boolean hasNext() {
			return crt != null && crt != TAIL;
		}

		@Override
		public E next() {
			E v = crt.val;
			crt = crt.next;
			return v;
		}
		
	    protected void clear() {
	    	//	清掉头尾针
	    	HEAD = TAIL = crt = null;
	    }
    	
    }

    /**
     *  双链节点
     */
    public static class Node<E> {
        public Node<E> prev, next;
        public E val;
        public Node(E val) {
            this.val = val;
        }
    }
}
